function moveNote(){

    var $noteLi = $("#note_list a.checked").parent();
    var noteId = $noteLi.data("noteId");
    var bookId = $("#moveSelect").val();

    $.ajax({
        url: base_url+"/note/move.do",
        type:"post",
        data:{"noteId":noteId,"bookId":bookId},
        dataType:"json",
        success:function (result) {
            if(result.status == 0){
                closeWindow()//关闭窗口
                $noteLi.remove()//移除笔记
                alert(result.msg)
            }
        },
        error:function () {
            alert("转移笔记失败")
        }
    })



}

function showMoveWindow(){

    $(".opacity_bg").show()
    var url = base_url+"/alert/alert_move.html"
    $("#can").load(url,function(){
        //在load页面载入#can之后，执行function逻辑
        //获取笔记本列表，生成对应option添加到下拉选框中
        var bookLis = $("#book_list li");
        for(var i=0;i<bookLis.length;i++){
            var bookName = $(bookLis[i]).text()
            var bookId = $(bookLis[i]).data("bookId");
            //拼一个option
            var s_opt = '<option value="'+bookId+'">'+bookName+'</option>'
            //将option最佳到moveSelect
            $("#moveSelect").append(s_opt)
        }
    })
    return false;
}


function viewShareNote(){
    var shareId = $(this).data("shareId")
    //console.log(shareId)
    $.ajax({
        url: base_url+"/note/loadShare.do",
        type:"post",
        data:{"shareId":shareId},
        dataType:"json",
        success:function (result) {
            if(result.status == 0){
                var title = result.data.cn_share_title
                var body = result.data.cn_share_body
                /*$("#noput_note_title").html(title)
                $("#noput_note_body").html(body)*/
                $("#input_note_title").val(title)
                //设置内容
                um.setContent(body)

            }
        },
        error:function () {
            alert("加载笔记信息失败")
        }

    })


}

function sureSearchShare(event){

    var code = event.keyCode;
    //console.log(code) 回车是13
    if(code == 13){
        var keyword = $("#search_note").val().trim()
        //发送ajax请求  /note/search.do    {"keyword":keyword}
        $.ajax({
            url: base_url+"/note/search.do",
            type: "post",
            dataType: "json",
            data:{"keyword":keyword},
            success:function (result) {
                //切换列表显示区 6
                showNoteList(6)
                var notes = result.data
                for(var i=0;i<notes.length;i++){
                    var title = notes[i].cn_share_title
                    var shareId = notes[i].cn_share_id
                    //拼一个li
                    var s_li= '<li class="online">'
                    s_li += '<a>'
                    s_li += '<i class="fa fa-book" title="online" rel="tooltip-bottom">'
                    s_li += '</i>'
                    s_li += title
                    s_li += '</a>'
                    s_li += '</li>'
                    //绑定shareId
                    var $li = $(s_li)
                    $li.data("shareId",shareId)
                    //将li添加到到搜索区列表
                    $("#search_list").append($li)
                }


            },
            error:function () {
                alert("检索笔记失败")
            }


        })

    }


}


/*切换显示列表*/
function showNoteList(index){
    //将所有列表隐藏
    $(".col-xs-3:not('#save_button_div')").hide();
    //将指定列表显示
    $("#pc_part_"+index).show()
}

/*切换显示列表*/
/*function showNoteList(index){
    //将所有列表隐藏
    $(".col-xs-3:not('#save_button_div')").hide();
    //将指定列表显示
    $("#pc_part_"+index).show()
}*/



function sureShareNote(){
    //alert("1111111111")
    var $li = $("#note_list a.checked").parent();
    var noteId = $li.data("noteId")
    //console.log(noteId)
    //     /note/share.do
    $.ajax({
        url: base_url+"/note/share.do",
        type: "post",
        data: {"noteId":noteId},
        dataType:"json",
        success:function (result) {
            alert(result.msg)
        },
        error: function () {
            alert("分享笔记失败")
        }
    })

    return false;//阻止冒泡
}


function showNoteMenu(){
    $("#note_list").on("click",".btn_slide_down",function(){
        //alert("11111111111111111")
        //隐藏所有笔记菜单
        $("#note_list .note_menu").hide()
        //点击当前按钮的菜单显示
        var $li = $(this).parents("li")
        var $menu = $li.find(".note_menu");
        $menu.slideDown(1000);
    })
    //追加鼠标对笔记菜单的处理
    $("#note_list").on("mouseover",".note_menu",function(){
        $(this).show() //保持菜单状态
    })
    $("#note_list").on("mouseout",".note_menu",function(){
        $(this).hide() //隐藏菜单
    })

}


function sureUpdateNote(){
//选择上笔记
    var $li = $("#note_list li a.checked").parent();
    //获取参数-noteId ,noteTitile, noteBody
    //console.log($li)
    if($li.length == 0){
        alert("请选择要保存的笔记")
    }else{
        //获取请求参数
        var noteId = $li.data("noteId");
        var noteTitle = $("#input_note_title").val().trim();
        var noteBody = um.getContent();
        //console.log(noteId,noteTitle,noteBody)
        //发送ajax请求： /note/update.do
        $.ajax({
            url: base_url+"/note/update.do",
            type:"post",
            data: {"noteTitle":noteTitle,"noteBody":noteBody,"noteId":noteId},
            dataType: "json",
            success:function (result) {
                if(result.status == 0){
                    //更新笔记li的名称
                    var str = '<i class="fa fa-file-text-o" title="online" rel="tooltip-bottom"></i>'
                        +noteTitle
                        +'<button type="button" class="btn btn-default btn-xs btn_position btn_slide_down"><i class="fa fa-chevron-down"></i></button>'

                    $li.find("a").html(str)
                    //提示成功
                    alert(result.msg)

                }

            },
            error:function () {
                alert("笔记保存失败")
            }

        })

    }

}




function sureAddNote(){

    //获取请求参数
    var noteTitle = $("#input_note").val().trim();
    var $li = $("#book_list a.checked").parent();
    var bookId = $li.data("bookId")
    //console.log(noteTitle,bookId)
    $.ajax({
        url: base_url+"/note/add.do",
        type: "post",
        data:{"bookId":bookId,"userId":userId,"noteTitle":noteTitle},
        dataType:"json",
        success: function (result) {
            //关闭对话框
            closeWindow()
            //添加笔记li
            var noteId = result.data
            createNoteLi(noteTitle,noteId)
            //提示成功
            alert(result.msg)
        },
        error: function () {
            alert("创建笔记失败")
        }

    })


}


function loadNoteDetail(){
    //设置选中状态及删除
    $("#note_list li a").removeClass("checked")
    $(this).find("a").addClass("checked");
    //获取请求参数
    var noteId = $(this).data("noteId");
    //alert(noteId)
    //发送ajax请求
    $.ajax({
        url: base_url+"/note/load.do",
        type: "post",
        data:{"id":noteId},
        dataType: "json",
        success:function (result) {
            if(result.status == 0){
                var noteTitle = result.data.cn_note_title
                var noteBody = result.data.cn_note_body
                //设置标题 input_note_title
                $("#input_note_title").val(noteTitle)
                //设置内容
                um.setContent(noteBody)
            }
        },
        error:function () {
            alert("加载笔记内容失败")

        }

    })
}




function loadBookNotes(){

    //alert("111111111")
    //当前笔记本设置为选中状态
    $("#book_list li a").removeClass("checked")
    $(this).find("a").addClass("checked")
    //获取bookId
    var bookId = $(this).data("bookId");
    //发送ajax请求
    $.ajax({
        url: base_url+"/note/loadnotes.do",
        type: "post",
        data: {"bookId":bookId},
        dataTpye: "json",
        success: function(result){
            if(result.status == 0){
                //获取笔记数组
                var notes = result.data
                //生成新的笔记列表前清除之前的列表
                $("#note_list").empty()
                //遍历数组，生成笔记列表
                for(var i=0;i<notes.length;i++){
                    var noteTitle = notes[i].cn_note_title
                    var noteId = notes[i].cn_note_id
                    //console.log(noteTitle,noteId)
                    //拼成一个li   标题  noteId
                    createNoteLi(noteTitle,noteId)
                }
            }
        },
        error:function(){
            alert("加载笔记列表失败")
        }

    });



}

function createNoteLi(noteTitle,noteId){
    var s_li = '<li class="online">'
    s_li += '<a>'
    s_li += '<i class="fa fa-file-text-o" title="online" rel="tooltip-bottom">'
    s_li += '</i>'+noteTitle
    s_li += '<button type="button" class="btn btn-default btn-xs btn_position btn_slide_down">'
    s_li += '<i class="fa fa-chevron-down">'
    s_li += '</i>'
    s_li += '</button>'
    s_li += '</a>'
    s_li += '<div class="note_menu" tabindex="-1">'
    s_li += '<dl>'
    s_li += '<dt><button type="button" class="btn btn-default btn-xs btn_move" title="移动至..."><i class="fa fa-random"></i></button></dt>'
    s_li += '<dt><button type="button" class="btn btn-default btn-xs btn_share" title="分享"><i class="fa fa-sitemap"></i></button></dt>'
    s_li += '<dt><button type="button" class="btn btn-default btn-xs btn_delete" title="删除"><i class="fa fa-times"></i></button></dt>'
    s_li += '</dl>'
    s_li += '</div>'
    s_li += '</li>'
    //将s_li转换为jq对象
    var $li = $(s_li);
    //绑定noteid
    $li.data("noteId",noteId)
    //将li添加到ul列表
    $("#note_list").append($li)
}










